package com.demo.project.service;

import com.demo.annotation.AutoWired;
import com.demo.annotation.Component;
import com.demo.project.dao.AccountDataServer;
import com.demo.project.dao.TeamDataServer;
import com.demo.project.entity.Account;
import com.demo.project.entity.Team;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class TeamService {
    @AutoWired
    private TeamDataServer dataServer;

    @AutoWired
    private AccountDataServer accountDataServer;

    public void setAccountDataServer(AccountDataServer accountDataServer) {
        this.accountDataServer = accountDataServer;
    }

    public void setDataServer(TeamDataServer dataServer) {
        this.dataServer = dataServer;
    }

    public List<Team> searchTeamByCondition(String s) {
        List<Team> teams = new ArrayList<>();
        List<String> teamIdByInfo = dataServer.getTeamIdByInfo(s);
        Map<String, Team> teamMap = dataServer.getTeamMap();
       teamIdByInfo.forEach(teamId->{
           Team team = teamMap.get(teamId);
           teams.add(team);
       });
        return  teams;
    }

    public String createTeam(Team team) {
        String teamId = dataServer.createTeam(team);
        String initiatorId = team.getInitiatorId();
        accountDataServer.addTeam(initiatorId,teamId);
        return teamId;
    }

    public Team getTeamById(String teamId) {
        return dataServer.search(teamId);
    }

    public boolean joinTeamByTeamId(String teamId, String uid) {
        Map<String, Team> teamMap = dataServer.getTeamMap();
        Team team = teamMap.get(teamId);
        if(team==null){
            return false;
        }
        if (team.getTeamMembers().contains(uid)) {
            return false;
        }
        Integer needNumber = team.getNeedNumber();

        if(needNumber==0){
            return false;
        }else {
            team.getTeamMembers().add(uid);
            team.setNeedNumber(team.getNeedNumber()-1);
            teamMap.replace(teamId,team);

            accountDataServer.addTeam(uid,teamId);
            return true;
        }

    }

    public boolean exitTeam(String teamId, String uid) {
        Map<String, Team> teamMap = dataServer.getTeamMap();
        Team team = teamMap.get(teamId);
        if(team==null){
            return false;
        }
        if(uid.equals(team.getInitiatorId())){
           return false;
        }
        List<String> teamMembers = team.getTeamMembers();
        boolean remove = teamMembers.remove(uid);
        if(remove){
            team.setNeedNumber(team.getNeedNumber()+1);
            teamMap.replace(teamId,team);
            accountDataServer.removeTeam(uid,teamId);
        }
        return remove;
    }

    public List<Account> getTeamNumberInfo(String teamId) {
        Map<String, Team> teamMap = dataServer.getTeamMap();
        Team team = teamMap.get(teamId);

        List<String> teamMembers = team.getTeamMembers();
        Map<String, Account> accountMap = accountDataServer.getAccountMap();
        List<Account> accounts = new ArrayList<>();
        teamMembers.forEach(id->{
            Account account = accountMap.get(id);
            accounts.add(account);
        });
        return accounts;
    }

    public String getTeamIdByUid(String uid){
        return accountDataServer.getTeamId(uid);
    }

    public boolean disBandTeam(String teamId, String uid) {
        Map<String, Team> teamMap = dataServer.getTeamMap();
        Team team = teamMap.get(teamId);
        if(team==null){
            return false;
        }
        if (team.getInitiatorId().equals(uid)) {
            List<String> teamMembers = team.getTeamMembers();
            accountDataServer.removeTeam(uid,teamId);//队长移除
            teamMembers.forEach(accountId->{
                accountDataServer.removeTeam(accountId,teamId);
            });
            dataServer.deleteTeam(teamId);
            return true;
        }else {
            return false;
        }
    }

    public Map<String, Team> getAllTeam() {
        return dataServer.getTeamMap();
    }
}
